// 示例
// const arr = [5, '9', '04', 11, 3, '2'];
// console.log(padArrayNumbers(arr)); // ['05', '09', '04', '11', '03', '02']

// let mySet = new Set([1, 2, 3]);
// const newArray = [4, 5, 6];
// // 使用 spread 操作符扩展数组元素到 Set 中
// mySet = new Set([...mySet, ...newArray]);
// console.log(mySet);  // Set { 1, 2, 3, 4, 5, 6 }

// 引入 path 模块，用于处理文件和目录路径
const path = require('path')
// 获取项目根目录
const ROOT_DIR = path.join(__dirname, '..')
// helpers 目录
const HELPERS_DIR = path.join(ROOT_DIR, 'helpers')

const { padArrayNumbers } = require(path.join(HELPERS_DIR, 'utils.js'))

const getLuckAndForecastIds = (redContinuousLimit2Group, greenReboundRedNonContinuousGroup, greenContinuousGroup) => {
  const a = redContinuousLimit2Group.map((item) => {
    return {
      ...item,
      forecast_type: 'red_continuous_limit_2'
    }
  })

  const b = greenReboundRedNonContinuousGroup.map((item) => {
    return {
      ...item,
      forecast_type: 'green_rebound_red_non_continuous'
    }
  })

  const c = greenContinuousGroup.map((item) => {
    return {
      ...item,
      forecast_type: 'green_continuous'
    }
  })

  const idsMap = {
    red_continuous_limit_2: [],
    green_rebound_red_non_continuous: [],
    green_continuous: []
  }

  // 合并排序
  const allGroups = [...a, ...b, ...c]

  // 用 Set 来避免重复的号码
  const luckySet = new Set()

  allGroups.forEach((group) => {
    const { id, sub_array, forecast_type } = group

    if (luckySet.size < 32) {
      // 一个号码一个号码去添加
      sub_array.forEach((number) => {
        if (luckySet.size < 32) {
          luckySet.add(number)
        }
      })
      idsMap[forecast_type].push(id)
    }
  })
  // console.log('luckySet Size', luckySet.size)

  const uniqueLuckyArray = [...luckySet]

  // 生成 1-49 的数组
  const all_numbers = Array.from({ length: 49 }, (_, i) => i + 1).map((num) => (num < 10 ? `0${num}` : `${num}`))

  // 幸运号码
  const lucky_array = padArrayNumbers(uniqueLuckyArray)

  // 杀号
  const kill_array = all_numbers.filter((item) => !lucky_array.includes(item))

  return {
    lucky_array: JSON.stringify(lucky_array),
    kill_array: JSON.stringify(kill_array),
    red_continuous_limit_2_group_ids: JSON.stringify(idsMap.red_continuous_limit_2),
    green_rebound_red_non_continuous_group_ids: JSON.stringify(idsMap.green_rebound_red_non_continuous),
    green_continuous_group_ids: JSON.stringify(idsMap.green_continuous)
  }
}

module.exports = { getLuckAndForecastIds }
